package com.qianbei.user;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.android.volley.Request.Method;
import com.bumptech.glide.Glide;
import com.qianbei.R;
import com.qianbei.common.base.BaseActivity;
import com.qianbei.common.init.GlobalConfig;
import com.qianbei.common.net.control.Constant;
import com.qianbei.common.net.control.ServerControl;
import com.qianbei.common.net.control.ServerControl.ServerListener;
import com.qianbei.common.net.control.ServerResult;
import com.qianbei.common.net.view.ProgressDialog;
import com.qianbei.user.model.UserMode;

public class EditUserrActivity extends BaseActivity {
	private TextView edit_name, edit_cunty, edit_city, edit_career;

	private String userID;
	String userMobile;
	View userlogin_view;
	private ImageView perceter_icon;

	private View user_set_icon2;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.user_edit_activity);
		edit_name = (TextView) findViewById(R.id.edit_name);
		edit_cunty = (TextView) findViewById(R.id.edit_cunty);
		edit_city = (TextView) findViewById(R.id.edit_city);
		edit_career = (TextView) findViewById(R.id.edit_career);
		setTitle("个人资料");
		mTitle.setrightTitle("编辑资料");
		
		GlobalConfig config = GlobalConfig.getInstance();
		if("1".equals(config.identity) && !config.can_update_info){
			mTitle.setrightTitle("审核中");
			mTitle.setRightTitleEnable(false);
		}
		
		perceter_icon = (ImageView) findViewById(R.id.perceter_icon);
		perceter_icon.setOnClickListener(this);

	}

	@Override
	protected void onResume() {
		super.onResume();
		Register();
	}

	@Override
	public void fClick(int id) {
		super.fClick(id);
		switch (id) {
		case R.id.tv_right:
			startActivity(ChangeUserActivity.class);
			break;

		default:
			break;
		}
	}

	/**
	 * 获取用户资料
	 */
	private void Register() {
		ServerControl sc = new ServerControl(Method.GET, user_info + "?");
		sc.serverListener = new ServerListener() {
			@Override
			public void serverFinish(ServerResult sr) {

				if (sr.isContinue) {
					UserMode mode = JSON.parseObject(
							sr.bodyData.optJSONObject("userInfo").toString(),
							UserMode.class);
					edit_name.setText(mode.name);
					edit_career.setText(mode.career);
					if(!((BaseActivity) mContext).isFinishing()) {
						Glide.with(mContext)
								.load(Constant.URL + mode.profile_image_url)
								.placeholder(R.drawable.ic_launcher)
								.into(perceter_icon);
					}
					edit_city.setText(mode.city);
					edit_cunty.setText(mode.country);
				}
			}
		};
		if("1".equals(GlobalConfig.getInstance().identity)){
			sc.startVolley();
			getStatus(); //
		}else{
			new ProgressDialog(mContext).startControl(sc);
		}
		
	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.perceter_icon:
			// startActivity(Login_regist_activity.class);
			break;

		}
	}


	public void getStatus(){
		ServerControl srControl = new ServerControl(Method.POST, Constant.user_seniority_info);
		srControl.serverListener = new ServerListener() {

			@Override
			public void serverFinish(ServerResult sr) {
				
				if (sr.isContinue) {
					GlobalConfig config = GlobalConfig.getInstance();
					config.canapply = sr.bodyData.optBoolean("can_apply_master");;
					config.can_update_info = sr.bodyData.optBoolean("can_update_info");
					config.apply_info = sr.bodyData.optBoolean("apply_info");
					
					if("1".equals(config.identity) && !config.can_update_info){
						mTitle.setrightTitle("审核中");
						mTitle.setRightTitleEnable(false);
					}else{
						mTitle.setrightTitle("编辑资料");
						mTitle.setRightTitleEnable(true);
					}
				}
			}
		};
		
		new ProgressDialog(mContext).startControl(srControl);
	}

}